package Model.DAO;

import Model.Entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao extends BaseDao{
    private static Long count = -1L;

    public UserDao() {
        super("User");
    }

    public Long userCount(Connection conn){
        if(count==-1L){
            try{
                count = 1L;
                PreparedStatement pre = conn.prepareStatement("SELECT * FROM user");
                ResultSet rst = pre.executeQuery();
                while(rst.next()) {
                    count++;
                }
                return count;
            }
            catch (Exception e){
                e.printStackTrace();
                return count;
            }
        }else {
            count++;
            return count;
        }
    }
    //添加新用户
    public boolean insertUser(User user)
    {
        Connection conn = null;
        try
        {
            conn = dataSource.getConnection();
            PreparedStatement pre = conn.prepareStatement("INSERT INTO user(id,name,password) VALUES(?,?,?)");
            pre.setLong(1, userCount(conn));
            pre.setString(2, user.getName());
            pre.setString(3, user.getPassword());

            pre.executeUpdate();
            conn.close();
            return true;
        }
        catch(SQLException e)
        {
            close(conn);
            return  false;
        }
    }
    //用户基本信息修改
    public boolean changeUser(User user){
        Connection conn = null;
        try{
            conn = dataSource.getConnection();
            PreparedStatement stm = conn.prepareStatement("UPDATE user SET phonenumber=?,email=?,payment=? WHERE name=?");
            stm.setString(1,user.getPhonenumber());
            stm.setString(2,user.getEmail());
            stm.setString(3,user.getPayment());
            int rst = stm.executeUpdate();
            conn.close();
            if(rst!=0)
                return true;
            else
                return false;
        }
        catch (Exception e){
            close(conn);
            return false;
        }
    }
    //修改用户密码
    public boolean changePwd(User user){
        Connection conn = null;
        try{
            conn = dataSource.getConnection();
            PreparedStatement stm = conn.prepareStatement("UPDATE user SET password = ? WHERE name=?");
            stm.setString(1,user.getPassword());
            stm.setString(2,user.getName());
            int rst = stm.executeUpdate();
            conn.close();
            if(rst!=0)
                return true;
            else
                return false;
        }
        catch (Exception e){
            close(conn);
            return false;
        }
    }

    //查询是否有该用户
    public int searchUser(User user)
    {
        int flag=0;//账号名不正确
        Connection conn = null;
        try{
            conn = dataSource.getConnection();
            PreparedStatement pre = conn.prepareStatement("SELECT * FROM user WHERE name='"+ user.getName() +"'");
            ResultSet rst = pre.executeQuery();
            if(rst.next())
            {
                flag = 2;//账号名正确
                String pwd = user.getPassword();
                if(pwd.equals(rst.getString(3))){
                    flag=1;//账号密码名字都正确
                }
            }
            conn.close();
            return flag;
        }
        catch(SQLException e)
        {
            close(conn);
            flag=0;
            e.printStackTrace();
            return flag;
        }
    }
}
